package com.lenovo.smarttraffic.ui.fragment;

import android.graphics.Color;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v4.app.DialogFragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.widget.TextView;

import com.bigkoo.pickerview.adapter.ArrayWheelAdapter;
import com.contrarywind.listener.OnItemSelectedListener;
import com.contrarywind.view.WheelView;
import com.lenovo.smarttraffic.R;

import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

/**
 * Created by Caleb.F
 * on 2020-03-10
 * at 22:03
 */
public class DateYearDialogFragment extends DialogFragment {
    private static final String DATA_INFO = "com.lenovo.smarttraffic.ui.fragment.DataPickerDialogFragment.argument";
    private String date_info;

    private TextView mTvYear;
    private TextView mTvDay;
    private TextView mTvWeek;
    private TextView mTvCancel;
    private TextView mTvConfirm;
    private WheelView mMyWheelView;

    private onItemSelectListener mOnItemSelectListener;

    public static DateYearDialogFragment newInstance(String argument) {
        Bundle bundle = new Bundle();
        bundle.putString(DATA_INFO, argument);
        DateYearDialogFragment dateYearDialogFragment = new DateYearDialogFragment();
        dateYearDialogFragment.setArguments(bundle);
        return dateYearDialogFragment;
    }

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        Bundle arguments = getArguments();
        date_info = arguments.getString(DATA_INFO);
    }

    @Override
    public void onStart() {
        super.onStart();
        getDialog().getWindow().setLayout(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
    }

    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        getDialog().getWindow().requestFeature(Window.FEATURE_NO_TITLE);
        return inflater.inflate(R.layout.fragment_dialog_year, container, false);
    }

    @Override
    public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        initView(view);
        initWheel();
    }

    private void initWheel() {
        mMyWheelView.setDividerType(WheelView.DividerType.WRAP);
        mMyWheelView.setTextSize(30);
        mMyWheelView.setDividerColor(Color.WHITE);
        mMyWheelView.setTextColorOut(Color.BLACK);
        mMyWheelView.setTextColorCenter(Color.parseColor("#008577"));

        List<String> strings = initData();
        mMyWheelView.setAdapter(new ArrayWheelAdapter(strings));
        mTvCancel.setOnClickListener(v -> dismiss());
        mMyWheelView.setOnItemSelectedListener(new OnItemSelectedListener() {
            @Override
            public void onItemSelected(int index) {
                mTvYear.setText(strings.get(index));
            }
        });
        mTvConfirm.setOnClickListener(v -> {
            mOnItemSelectListener.getYear(strings.get(mMyWheelView.getCurrentItem()));
            dismiss();
        });
    }

    private List<String> initData() {
        ArrayList<String> strings = new ArrayList<>();
        for (int i = 2015; i < 2025; i++) {
            strings.add(String.valueOf(i));
        }
        return strings;
    }

    private void initView(View convertView) {

        mTvYear = convertView.findViewById(R.id.tv_year);
        mTvDay = convertView.findViewById(R.id.tv_day);
        mTvWeek = convertView.findViewById(R.id.tv_week);
        mMyWheelView = convertView.findViewById(R.id.my_wheel_view);
        mTvCancel = convertView.findViewById(R.id.tv_cancel);
        mTvConfirm = convertView.findViewById(R.id.tv_confirm);
        mTvYear.setText(date_info.split(",")[0]);
        mTvDay.setText(date_info.split(",")[1]);
        mTvWeek.setText(date_info.split(",")[2]);
    }

    public void setonItemSelectListener(onItemSelectListener onItemSelectListener) {
        mOnItemSelectListener = onItemSelectListener;
    }

    public interface onItemSelectListener {
        void getYear(String year);

    }

}
